package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @RequiresPermissions(value={"role:view","角色视图"},logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "role/role";
    }

    @RequiresPermissions(value={"role:query","角色列表"},logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return roleService.query(qo);
    }

    @RequiresPermissions(value={"role:selectAll","角色全选"},logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Role> selectAll() {
        return roleService.selectAll();
    }

    @RequiresPermissions(value={"role:saveOrUpdate","角色编辑/新增"},logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Role entity) {
        try {
            roleService.saveOrUpdate(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }
    @RequiresPermissions(value={"role:delete","角色删除"},logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            roleService.deleteByPrimaryKey(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value={"employee:getRoleByEmployeeId","员工回显角色"},logical = Logical.OR)
    @RequestMapping("getRoleByEmployeeId")
    @ResponseBody
    public List<Long> getRoleByEmployeeId(Long employeeId) {
        return roleService.getRoleByEmployeeId(employeeId);
    }

}
